from typing import *


class Solution:
    def maximumLength(self, nums: List[int]) -> int:
        ans = 0
        cnt = Counter(nums)
        for k, v in cnt.items():
            c, u, nxt = 0, 0, k
            while nxt in cnt.keys() and cnt[nxt] - u > 1:
                c += 2
                nxtval = nxt * nxt
                u += 2
                if nxtval != nxt:
                    u = 0
                nxt = nxtval
            if nxt in cnt.keys() and cnt[nxt] - u == 1:
                c += 1
            else:
                c -= 1
            ans = max(ans, c)

        return ans


s = Solution()
print(s.maximumLength([1, 1]))
print(s.maximumLength([5, 4, 1, 2, 2]))
print(s.maximumLength([1, 1, 1, 1, 1, 1, 1, 1, 2]))
print(s.maximumLength([5, 4, 1, 2, 2]))
print(s.maximumLength([1, 3, 2, 4]))
print(s.maximumLength([2, 4, 16, 4, 2, 3, 9, 3]))
